{This unit and the accompanying form (SpecForm) handle the user input for }
{adding, changing, or deleting species from data set S. }
{In addition to standard Delphi Units, it uses the Editor routines and }
{the Titrator definition file TitrDefs.}

unit SpecEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TitrDefs, StdCtrls, ExtCtrls, Grids, Buttons, Editor;

type
  TSpecForm = class(TForm)
    Label1: TLabel;
    NameEdit: TEdit;
    Label2: TLabel;
    LogKEdit: TEdit;
    STypeGroup: TRadioGroup;
    StoichiometryGrid: TStringGrid;
    AcceptBtn: TBitBtn;
    DeleteBtn: TBitBtn;
    DelHEdit: TEdit;
    DelSEdit: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    ThermoBox: TCheckBox;
    Label3: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    HelpBtn: TButton;
    Label10: TLabel;
    StdDevEdit: TEdit;

    procedure FormActivate(Sender: TObject);
    procedure AcceptBtnClick(Sender: TObject);
    procedure DeleteBtnClick(Sender: TObject);
    procedure LogKEditExit(Sender: TObject);
    procedure STypeGroupExit(Sender: TObject);
    procedure NameEditExit(Sender: TObject);
    procedure StoichiometryGridClick(Sender: TObject);
    procedure DelHEditExit(Sender: TObject);
    procedure DelSEditExit(Sender: TObject);
    procedure ThermoBoxClick(Sender: TObject);
    procedure HelpBtnClick(Sender: TObject);
    procedure StdDevEditExit(Sender: TObject);
   

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  SpecForm: TSpecForm;

  
{******************************************************************************}
{******************************************************************************}


implementation

Uses TitrHelp;

{$R *.dfm}


{Procedure WriteScreen writes the contents of the species being edited }
{ (EdSpec) to the screen as readable text }

Procedure WriteScreen;
Var com : integer;          {component counter for display loop}

begin
   With SpecForm do begin
      For com := 1 to S.NComp do begin
         StoichiometryGrid.Cells[com, 0] := S.Comps[com].Name;
         StoichiometryGrid.Cells[com, 1] := IntToStr(EdSpec.coeff[com]);
        end;
      NameEdit.Text := EdSpec.Name;
      STypeGroup.ItemIndex := Integer(EdSpec.SType);
      LogKEdit.Text := FloatToStrF(EdSpec.LogK, ffFixed, 5, 4);
      StdDevEdit.Text := FloatToStrF(EdSpec.StdDev, ffFixed, 5, 4); 
      DelHEdit.Text := FloatToStrF(EdSpec.DeltaH, ffFixed, 5, 4);
      DelSEdit.Text := FloatToStrF(EdSpec.DeltaS, ffFixed, 5, 4);
      ThermoBox.Checked := EdSpec.KCalc;
   end;
end;

{Formactivate is called whenever SpecForm is activated, and writes }
{approprate information to the form }

procedure TSpecForm.FormActivate(Sender: TObject);
begin
  WriteScreen;
  StoichiometryGrid.ColCount := S.NComp+1;            {Only display needed cols}
   If (EdMode = Add) then begin
      SpecForm.Caption := 'Add New Species';
      DeleteBtn.Caption := 'Cancel';
     end
      else begin
         SpecForm.Caption := 'Component Editor';
         DeleteBtn.Caption := 'Delete';
        end;
end;

{AcceptBtnClick occurs when user presses the upper button labelled Accept.}
{If the species being edited already exists, this indicates that the changes
{       made to EdSpec should be written to the selected species}
{If the species is new, this is indicates the new species should be added}

procedure TSpecForm.AcceptBtnClick(Sender: TObject);
begin
    If (EdMode = Edit) then
     If (MessageDlg('Accept changes to this species?',
            mtConfirmation, [mbOK, mbCancel],0) = mrCancel) then
               EdMode := None;          {not confirmed}

   If (EdMode = Add) then
      If (MessageDlg('Add this species to the data set?',
            mtConfirmation, [mbOK, mbCancel],0) = mrCancel) then
               EdMode := None;          {not confirmed}

   SpecForm.Close;
end;

{DeleteBtnClick occurs when user presses the lower key. }
{If the species being edited already exists, this is a delete key,
{       and the user is asked to confirm deletion (or no change is made).}
{If the species is new, this is a cancel action key and no change is made}

procedure TSpecForm.DeleteBtnClick(Sender: TObject);
begin
    If (EdMode = Edit) then begin        {If this was an existing species }
        Edmode := Delete;               {this is a delete key}
        {Now confirm the deletion..}
        If (MessageDlg('Delete this species?',
            mtConfirmation, [mbOK, mbCancel],0) = mrCancel) then
               EdMode := None;          {not confirmed}
       end
       else EdMode := None;             {Else this was an addition, and key}
                                        {indicates cancelling the addition}

       SpecForm.Close;                  {exit this form}
end;


{***************************************************************************}
{     The following set of procedures respond to user inputs by             }
{     Checking the whether new, valid values have been entered and          }
{     saving valid changes to EdSpec.                                       }
{***************************************************************************}

{Log K is currently restricted to the range +500 to -500 }

procedure TSpecForm.LogKEditExit(Sender: TObject);
begin
   LogKEdit.Text := WriteRealEdit(LogKEdit.Text, EdSpec.LogK, -500, 500);
end;

{Currently only two types are implemented- dissolved and potential precipitate }

procedure TSpecForm.STypeGroupExit(Sender: TObject);
begin
  EdSpec.SType := SpecType(STypeGroup.ItemIndex);
end;

{All strings are legal names}

procedure TSpecForm.NameEditExit(Sender: TObject);
begin
   EdSpec.Name := NameEdit.text;
end;

{Stoichiometric coefficients are edited via an InputBox call, which is less }
{convenient than direct editing in the grid but easier to code.  As a reminder-
{only integer values are permitted }

procedure TSpecForm.StoichiometryGridClick(Sender: TObject);
Var   com : integer;
      result, code : integer;
      dummy, UserInput : string;

begin
   com := StoichiometryGrid.Col;
   dummy := 'For ' + S.Comps[com].name;
   UserINput := InputBox('Stoichiometric Coefficient', dummy,
             IntToStr(EdSpec.Coeff[com]));

   Val(UserInput, Result, code);
   If (code = 0) then begin
      EdSpec.Coeff[com] := Result;
      StoichiometryGrid.Cells[com, 1] := IntToStr(Result);
     end;
end;                            {End of StoichiometryGridClick}

procedure TSpecForm.DelHEditExit(Sender: TObject);
begin
   DelHEdit.Text := WriteRealEditFix(DelHEdit.Text, EdSpec.DeltaH, -300,300);
end;

procedure TSpecForm.DelSEditExit(Sender: TObject);
begin
   DelSEdit.Text := WriteRealEditFix(DelSEdit.Text, EdSpec.DeltaS, -30000,30000);
end;

procedure TSpecForm.ThermoBoxClick(Sender: TObject);
begin
   If ThermoBox.Checked then EdSpec.KCalc := True
     else EdSpec.KCalc := False;
end;

procedure TSpecForm.HelpBtnClick(Sender: TObject);
begin
   HelpIndex := 5;
   TitrHelpForm.ShowModal;
end;

procedure TSpecForm.StdDevEditExit(Sender: TObject);
begin
    StdDevEdit.Text := WriteRealEdit(StdDevEdit.Text, EdSpec.StdDev, 0, 10);
end;

end.                            {End of SpecEdit}
